package com.kjc.demo.basic.searchalgorithm;

public class BinarySearch {
	
	public static int binarySearch(int[] arr,int value , int length) {
		int index = -1;
		int left = 0;
		int right = length-1;
		int mit = 0;
		while(left <= right) {
			mit = (left + right)/2;
			if(arr[mit] == value) {
				return mit;
			}
			if(arr[mit] > value) {
				right = mit - 1;
			}
			if(arr[mit] < value) {
				left = mit + 1;
			}
		}
		return index;
	}
	
//	public static int binarySearch(int[] arr,int value,int low,int high) {
//		int mid = low + (high+ low)/2;
//		if(arr[mid] == value) {
//			return mid;
//		}
//		if(arr[mid] > value) {
//			return binarySearch(arr,value,low,mid-1);
//		}
//		if(arr[mid] < value) {
//			return binarySearch(arr,value,mid+1,high);
//		}
//		
//	}
	
	
	
	public static void main(String[] args) {
		int[] arr = {2,3,4,5,6,7,9,22};
		System.out.println(binarySearch(arr,22,arr.length));
	}
}
